""" Expand a cell by changing its lattice parameters. """

import sys, getopt

from utils_vasp import structure

def usage():
    print(sys.argv[0] + "-s number -o outputfile structurefile")
    print("""
    -s number, --scale=number
        scale axis a, b, c by number. It must be positive. Default 1.
    -o outputfile, --output=outputfile
        write the new structure to outputfile. Default "POSCAR_temp".
    -h, --help
        print this message.
    """)


def check_positive(v):
    """ if v is not positive, then show error and exit(3)."""
    if v<=0.0 :
        print("The argument must be a positive number,"
              "but you have supplied "+str(v)+ " which is not."
              "Please double check and retry.")
        sys.exit(3)


args_short = "hs:o:"
args_long  = [
    "help",
    "scale=",
    "output="]
try:
    opts, args = getopt.getopt(sys.argv[1:], args_short, args_long)
except getopt.GetoptError as err:
    print(str(err))
    usage()
    sys.exit(1)

outputfile = "POSCAR_temp"
expansion_a = 1.0

for o, a in opts:
    if o in ("-h", "--help"):
        usage()
        sys.exit(0)
    elif o in ("-s", "--scale"):
        scaling_ratio = float(a)
        check_positive(scaling_ratio)
        expansion_a = scaling_ratio
    elif o in ("-o", "--output"):
        outputfile = str(a)
    else:
        print("Unrecognized option:" + o)
        print("I will continue. You should be warned about it.")

# There should be only one argument left.
if len(args) != 1:
    print("The program needs one argument as input file name.")
    print("It may be POSCAR or CONTCAR or other filenames.")
    usage()
    sys.exit(4)
inputFileName = args[0]

S = structure.VASP_structure()
S.read(inputFileName)
S.expandCell(expansion_a, (0.5, 0.5, 0.5))

if outputfile is None:
    print("You haven't designate an output file.")
    print("The structure will be shown on screen.")
    S.write(sys.stdout)
else:
    S.write(outputfile)

